package com.example.demo.controller;

import com.example.demo.entity.Instorage;
import com.example.demo.result.RestBean;
import com.example.demo.service.InstorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/instorage")
public class InstorageController {

    @Autowired
    private InstorageService instorageService;

    @PostMapping("/create")
    public RestBean<Instorage> createInstorage(@RequestBody Instorage instorage) {

        int result = instorageService.createInstorage(instorage);

        if (result == 1) {
            System.out.println("添加成功！");
            return RestBean.success(instorage);
        }
        System.out.println("添加失败！");
        return RestBean.failure(201,null);
    }

    @PostMapping("/delete")
    public RestBean<String> deleteInstorage(@RequestBody Instorage instorage) {

        int result = instorageService.deleteInstorage(instorage.getIid());

        if (result == 1) {
            return RestBean.success("删除成功！");
        }
        return RestBean.failure(404,"删除失败！");

    }

    @PostMapping("/findone")
    public RestBean<List<Instorage>> findInstorage(@RequestBody Instorage instorage) {

        List<Instorage> instorages = new ArrayList<>(5);
        System.out.println(instorage);
        Instorage instorage1 = instorageService.findInstorage(instorage.getIid());
        System.out.println(instorage1);
        instorages.add(instorage1);
        return RestBean.success(instorages);
    }

    @GetMapping("/findall")
    public RestBean<List<Instorage>> findAllinstorage() {

        List<Instorage> instorages = instorageService.findAllinstorages();

        if (!instorages.isEmpty()) {
            System.out.println("查询成功！");
            return RestBean.success(instorages);
        }
        else {
            System.out.println("查询失败！");
            return RestBean.failure(404,null);
        }
    }

}
